
Global TilesetList : TList = New TList


Type Tileset Extends Asset

	Field Frames : Int
	Field Cell_W : Int
	Field Cell_H : Int
	Field Tileset_Img : TImage
	Field Mask_R : Int
	Field Mask_G : Int
	Field Mask_B : Int
	
	Method New()
	
		AssetType = ASSET_TILESET
		ListAddLast(TilesetList,Self)
	End Method

	Method Load()
	
		Local tiledoc : xmlDocument = New xmlDocument
		tiledoc.Load(FileBank)
		Local tileroot : xmlNode = tiledoc.root()		
		
		Local TilesetNode : xmlNode = tileroot.FindChild("Tileset")
		Cell_W = Int(TilesetNode.Attribute("CellW").value)
		Cell_H = Int(TilesetNode.Attribute("CellH").value)
		Frames = Int(TilesetNode.Attribute("Frames").value)
		Mask_R = Int(TilesetNode.Attribute("MaskR").value)
		Mask_G = Int(TilesetNode.Attribute("MaskG").value)
		Mask_B = Int(TilesetNode.Attribute("MaskB").value)	
		Flags = Int(TilesetNode.Attribute("Flags").value)		
		AssetPath = TilesetNode.Attribute("Path").value
		
		DebugLog "New Filebank "+Replace("Projects/"+CurrentProject+"/"+AssetPath,"\","/")
		FileBank = LoadBank(Replace("Projects/"+CurrentProject+"/"+AssetPath,"\","/"))
		Tileset_Img = LoadAnimImage( FileBank,Cell_W,Cell_H,0,Frames,Flags )
	
	
	End Method


	Method Save()
	
		Local tiledoc : xmlDocument = New xmlDocument
		Local tileroot : xmlNode = tiledoc.root()
		
		
		Local TilesetNode : xmlNode = tileroot.AddNode("Tileset",AS_CHILD)
		TilesetNode.Attribute("CellW").value = Cell_W
		TilesetNode.Attribute("CellH").value = Cell_H
		TilesetNode.Attribute("Frames").value = Frames
		TilesetNode.Attribute("MaskR").value = Mask_R
		TilesetNode.Attribute("MaskG").value = Mask_G
		TilesetNode.Attribute("MaskB").value = Mask_B
		TilesetNode.Attribute("Flags").value = Flags
		TilesetNode.Attribute("Path").value = AssetPath
		DebugLog "Asset Path: "+AssetPath
		If Lower(ExtractExt(AssetPath)) = "tset"
			tiledoc.Save(Replace("Projects\"+CurrentProject+"\"+AssetPath,"\","/"))
		Else
			tiledoc.Save(Replace("Projects\"+CurrentProject+"\"+AssetPath+".tset","\","/"))
		EndIf
	
	
	End Method
	

	Method TilesetFromBank(Bank : TBank, frms : Int, ww : Int, hh : Int, fflags : Int)
	
		If Bank = Null Then Return False
		
		DebugLog "Have Bank"
		
		FileBank = Bank
		Frames = frms
		Cell_W = ww
		Cell_H = hh
		Flags = fflags
		Tileset_Img = LoadAnimImage( Bank,Cell_W,Cell_H,0,Frames,Flags )
		
		If Tileset_Img = Null Then 
			Return False
		Else
			Return True
		EndIf
	End Method

End Type

